// pages/unoccupiedAndHaveRent/unoccupiedAndHaveRent.js
var common = require('../../utils/common.js');
const app = getApp();
var that = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {

    status: '',//状态0为已生成账单，1未生成 目前未使用
    house_id: '',//房屋id
    showtab: 0,  //顶部选项卡索引
    tabnav: {//顶部选项卡数据7
      tabnum: 2,
      tabitem: [
        {
          "id": 0,
          "type": "",
          "text": "已租",
          "count": ''
        },
        {
          "id": 1,
          "type": "A",
          "text": "待租",
          "count": ''
        }
      ]
    },
    rentalHouse: [],
    unRentalHouse: [],
  },

  setTab: function (e) { //设置选项卡选中索引
    const edata = e.currentTarget.dataset;
    this.setData({
      showtab: edata.tabindex,
    })
    if (edata.tabindex == 0) {
      //已租
      this.setData({
        status: '1'
      })
      // if (this.data.forRentDataList.length < 1) {
      //   console.log("已租")
      //   this.getDataList();
      // }
    }
    else if (edata.tabindex == 1) {
      //未租
      this.setData({
        status: '0'
      })
      // if (this.data.waitingForRentDataList.length < 1) {
      //   this.getDataList();
      // }
    }
    this.initData();
  },
  //点击右边的展开图标
  clickRIcon: function (e) {
    var index = e.currentTarget.dataset.index;
    var res = [];
    if (this.data.showtab == 0) {
      res = this.data.rentalHouse;
      res[index].check = !res[index].check;
      this.setData({
        rentalHouse: res
      })
    } else {
      res = this.data.unRentalHouse;
      res[index].check = !res[index].check;
      this.setData({
        unRentalHouse: res
      })
    }

  },
  //初始化数据
  initData: function () {
    if (that.data.showtab == 0 && that.data.rentalHouse.length < 1) {
      this.getRentalHouse();
    } else if (that.data.showtab == 1 && that.data.unRentalHouse.length < 1) {
      this.getUnRentalHouse();
    }
  },

  //头部统计
  getDataTotal: function () {
    common._get('/wx/landlord/center/rental/total', {},
      //成功
      function (res) {
        if (res.data.success) {
          that.data.tabnav.tabitem[0].count = res.data.rentalNum;
          that.data.tabnav.tabitem[1].count = res.data.unrentalNum;
          that.setData({
            tabnav: that.data.tabnav
          })
        }
      });
  },
  //获取已出租房源
  getRentalHouse: function () {
    common._get('/wx/landlord/center/rental/list', { status: 1 },
      //成功
      function (res) {
        console.log('ssssssss');
        console.log(res.data);
        if (res.data.success) {
          for (var i in res.data.data) {
            res.data.data[i].check = false;
          }
          that.setData({
            rentalHouse: res.data.data
          })
        }
      });
  },
  //获取未租房源
  getUnRentalHouse: function () {
    common._get('/wx/landlord/center/rental/list', { status: 0 },
      //成功
      function (res) {
        console.log('未租房源测试');
        console.log(res.data);
        if (res.data.success) {
          for (var i in res.data.data) {
            res.data.data[i].check = false;
          }
          that.setData({
            unRentalHouse: res.data.data
          })
        }
      });
  },
  //点击某个房间
  roomitemclick: function (e) {
    // var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../waitingRentRoom/waitingRentRoom?room_id=' + id
    })
  },
  addHouse:function () {
    wx.navigateTo({
      url: '../addHouse/addHouse',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      showtab: options.showtab
    })
    that = this;
    this.getDataTotal();
    this.initData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log('-监听页面初次渲染完成')
    //首次需要加载未租房源
    this.getUnRentalHouse();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var refresh = app.getRefresh();
    if (refresh) {
      this.getDataTotal();
      this.getRentalHouse();
      this.getUnRentalHouse();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getDataTotal();
    this.getRentalHouse();
    this.getUnRentalHouse();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})